<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/19 0019
 * Time: 下午 2:50
 */
namespace app\common\validate;

use app\lib\exception\ParameterException;

class MakeGroup extends BaseValidate{
    protected $product = [
        [
            'productId' => 3,
            'count' =>3
        ]

    ];
    protected $rule = [
        'id' => 'require|isNotEmpty',
        'grouptype' => 'require|checkgrouptype',
        'products' => 'checkProducts',
        'orderType' => 'require|checkgrouptype',

    ];

    protected $message = [
        'id' => '商户发布的团购id不能为空',
        'grouptype' => '团购方式只能是1或2,1参团，2开团',
        'orderType' => '缺少订单类型，1配送，2自提',
    ];
    protected $singRule = [
        'product_id' => 'require|isNotEmpty',
        'count' => 'require|isPositiveInteger'
    ];

    protected function checkProducts($values){
        if(!is_array($values)){
            throw new ParameterException([
                'msg' => '商品参数不合法'
            ]);
        }

        if(empty($values)){
            throw new ParameterException([
                'msg' => '商品列表不能为空'
            ]);
        }



        foreach ($values as $value){
            $this->checkProduct($value);
        }

        return true;
    }

    protected function checkgrouptype($value){
        if($value == 1 || $value == 2){
            return true;
        }else{
            return false;
        }
    }
    protected function checkProduct($value){
        $validate = new BaseValidate($this->singRule);
        $result = $validate->check($value);
        if(!$result){
            throw new ParameterException([
                'msg' => '商品列表参数错误'
            ]);
        }
    }
}